<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require './vendor/autoload.php';

class FFmpeg {
    private $ffmpeg;

	public function __construct() {
        // $ffmpeg = FFMpeg\FFMpeg::create([
        //     'ffmpeg.binaries'  => __DIR__ .  DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . '\ffmpeg\ffmpeg.exe',
        //     'ffprobe.binaries' => __DIR__ .  DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . '\ffmpeg\ffprobe.exe'
        // ]);
        // $this->ffmpeg = $ffmpeg;

        // var_export(strtoupper(substr(PHP_OS, 0, 3)));exit;

        // 判断操作系统
        if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $configArr = array(
                'ffmpeg.binaries'  => __DIR__ .  DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . '\ffmpeg\ffmpeg.exe',
                'ffprobe.binaries' => __DIR__ .  DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . '\ffmpeg\ffprobe.exe'
            );
        } else {
            $configArr = array(
                'ffmpeg.binaries' => '/usr/local/bin/ffmpeg',
	            'ffprobe.binaries' => '/usr/local/bin/ffprobe',
            );
        }
        $ffmpeg = FFMpeg\FFMpeg::create($configArr);

        $this->ffmpeg = $ffmpeg;
    }

    // 视频截图
    public function snapshot($videoUrl) {
        $video = $this->ffmpeg->open($videoUrl);

        // 本地文件根目录
        $dirLocal = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'local' . DIRECTORY_SEPARATOR;
        // 获取本地文件类型目录
        $dirType = $dirLocal . 'image' . DIRECTORY_SEPARATOR;
        $dirY = $dirType . date('Y') . DIRECTORY_SEPARATOR;
        $dirM = $dirY . date('m') . DIRECTORY_SEPARATOR;
        $dirD = $dirM . date('d') . DIRECTORY_SEPARATOR;

        // 判断是否已存在目录，没有就创建目录
        is_dir($dirType) || mkdir($dirType);
        is_dir($dirY) || mkdir($dirY);
        is_dir($dirM) || mkdir($dirM);
        is_dir($dirD) || mkdir($dirD);
        // 文件类型后缀
        $suffix = 'jpg';
        $randFileName = uniqid() . rand(1000, 9999) . '.' . $suffix; 
        $fileName = $dirD . $randFileName; 
        // 文件url路径
        $webUrl = BASE_URL . 'local/image' . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/' . $randFileName;

        // var_export($video);
        $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1))->save($fileName);
        // 返回截图路径
        return $webUrl;
    }

    // 视频转码压缩
    public function compress($videoUrl, $saveAsLink) {
        $video = $this->ffmpeg->open($videoUrl);
        $video->filters()->resize(new FFMpeg\Coordinate\Dimension(360, 640), FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_FIT, true);
        // 获取上传路径
        // 本地文件video目录
        $dirLocal = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'local/video';

        // 通过weburl获取文件路径
        $temp = explode('local/video', $saveAsLink)[1];
        $fileName = $dirLocal . $temp;
    // var_dump($saveAsLink);exit;
        // 旧库没有libfaac，故得传参数
        $video->save(new FFMpeg\Format\Video\X264('aac'), $fileName);
    }

}